<?php
$v_status_compare = 0;	
if(onegate_is_approve_leader_for_recordtype($v_staff_id,$_SESSION['onegate_current_recordtype'],$_SESSION["recordtype"])){
	$v_status_compare = 1;	
}
// Luu dieu kien loc theo linh vuc 
$v_current_item_id = '';
if(isset($_REQUEST['hdn_current_item_id'])){
	$v_current_item_id = $_REQUEST['hdn_current_item_id'];
}
if ($v_receive_date !=""){
	$v_receive_date_filter = $v_receive_date;
}else{
	$v_receive_date_filter = "";
}
if(isset($_REQUEST['hdn_received_date_filter'])){
	$v_receive_date_filter = $_REQUEST['hdn_received_date_filter'];
} 
$v_recordtype_filter = $_SESSION['onegate_current_recordtype'];
//--------------------------- nieuvv 220508----------------------------
if(isset($_REQUEST['sel_statustype'])){
	$v_sel_statustype = $_REQUEST['sel_statustype'];
}
//echo $v_sel_statustype ."<br>";
$_SESSION['onegate_current_status'] = $v_sel_statustype;
$v_session_sel_statustype = $_SESSION['onegate_current_status'];
$v_appointed_date = date("d/m/Y");
//echo $v_session_sel_statustype."<br>";
//echo $fuseaction . " trong file record-lib/qry_all_record.php line 24";
//----------------------- the end nieuvv ------------------------------
$v_staff_id =  $_SESSION['staff_id'];
if (!isset($_SESSION['staff_id'])||is_null(($_SESSION['staff_id']))){
	$v_staff_id = 0;
}
switch($_ISA_CURRENT_MODUL_CODE) {
	case "HANDLE";
		if(onegate_is_permission_update_rate_of_progress_for_recordtype($v_staff_id,0,$_SESSION["recordtype"])){
			$v_role = "";
		}else{
			$v_role = "THU_LY";
		}		
		break;
	default:
		$v_role = "";
		break;
}
if ($v_is_granted_view_record){
	$v_role = "";
}

//nieuvv--111108--hien thi cong viec dac biet
$v_type_spec_work = "";
if(isset($_REQUEST['type_spec_dsp']))
{
 $v_type_spec_work = $_REQUEST['type_spec_dsp'];
}
if ($v_type_spec_work == "")
{
 if(isset($_REQUEST['hdn_spec_worktype']))
 {
  $v_type_spec_work = $_REQUEST['hdn_spec_worktype'];
 }
}

//the-end-nieuvv--111108--
$v_status_of_handover = -10;
$v_status_of_assigning = -10;
$v_status_of_handle_approve = -10;
$v_status_of_chaiman_approve = -10;
//$v_order_clause = "ORDER BY C_RECEIVED_DATE ASC";
$v_order_clause = $_REQUEST['hdn_order_clause'];
//$v_order_clause_cmnd = 'order by cmnd  asc'; // NIEUVV 160508
//Kiem tra co phai la module APPROVING hay khong va detail_status la rong
// begin remmed by vunh 25042008
/*if (($_ISA_CURRENT_MODUL_CODE=="APPROVING")&&(trim($v_detail_status_compare)=="")){
	if (onegate_is_approve_leader_type($_SESSION['staff_id'], $_SESSION['onegate_current_recordtype'], $_SESSION["recordtype"])==0){
		$v_detail_status_compare = " And C_DETAIL_STATUS = 30 ";
	}
	if (onegate_is_approve_leader_type($_SESSION['staff_id'], $_SESSION['onegate_current_recordtype'], $_SESSION["recordtype"])==1){
		$v_detail_status_compare = " And (C_DETAIL_STATUS = 30 Or C_DETAIL_STATUS = 40) ";
	}
	if (onegate_is_approve_leader_type($_SESSION['staff_id'], $_SESSION['onegate_current_recordtype'], $_SESSION["recordtype"])==2){
		$v_detail_status_compare = " And C_DETAIL_STATUS = 40  ";
	}
	if (onegate_is_approve_leader_type($_SESSION['staff_id'], $_SESSION['onegate_current_recordtype'], $_SESSION["recordtype"])==3){
		$v_detail_status_compare = " And C_DETAIL_STATUS = 23  ";
	}
}*/
// end remmed by vunh 25042008
// begin added by vunh 25042008
if (($_ISA_CURRENT_MODUL_CODE=="APPROVING")&&(trim($v_detail_status_compare)=="")){
	$current_record_type = get_current_record_type($_SESSION['onegate_current_recordtype'], $_SESSION["recordtype"]);
	//var_dump($current_record_type);exit;
	$v_detail_status_compare = "And (1=1 ";
	$operator = "Or ";
	$first_conditional = 0;
	if (is_handle_leader_first_time($_SESSION['staff_id'], $current_record_type)){
		//$v_detail_status_compare = " And (C_DETAIL_STATUS = 30 ";
		$operator = "And (";
		$v_detail_status_compare = $v_detail_status_compare.$operator."(C_DETAIL_STATUS = 23 And FK_HANDLE_LEADER_FIRST_TIME = ".$v_staff_id.")";
		$first_conditional = 1;
	}
	if (is_handle_leader($_SESSION['staff_id'], $current_record_type)){
		if ($first_conditional == 1) {
			$operator = "Or ";
		} else {
			$operator = "And (";
		}
		$v_detail_status_compare = $v_detail_status_compare.$operator."(C_DETAIL_STATUS = 30 And FK_HANDLE_LEADER = ".$v_staff_id.")";
		$first_conditional = 1;
	}
	if (is_approve_leader($_SESSION['staff_id'], $current_record_type)){
		if ($first_conditional == 1) {
			$operator = "Or ";
		} else {
			$operator = "And (";
		}
		$v_detail_status_compare = $v_detail_status_compare.$operator."(C_DETAIL_STATUS = 40 And FK_APPROVE_LEADER = ".$v_staff_id.")";
		$first_conditional = 1;
	}
	$v_detail_status_compare = $v_detail_status_compare.")";
	if ($first_conditional == 1) {
		$v_detail_status_compare = $v_detail_status_compare.")";
	}	
}
// end remmed by vunh 25042008
$v_receive_date_filter = "";
//Lay so dong tren mot danh sach
$v_limit_row = _CONST_NUMBER_OF_ROW_PER_LIST;
//session_start();
//$_SESSION['v_session_limit_num_page'] = $v_limit_row;
if(isset($_REQUEST['hdn_record_number_page'])&&$_REQUEST['hdn_record_number_page']!=""){
	$v_limit_row = $_REQUEST['hdn_record_number_page'];
	//$_SESSION['v_session_limit_num_page'] = $v_limit_row;
} 
//echo '$v_limit_row'.$v_limit_row.'<br>';

//Lay trang thu x
$v_page = 1;
//session_start();
//$_SESSION['v_session_page'] = $v_page;
if(isset($_REQUEST['hdn_page'])&&$_REQUEST['hdn_page']!=""){
	$v_page=$_REQUEST['hdn_page'];	
	//$_SESSION['v_session_page'] = $v_page;
}
# begin vunh added 23/04/2008
	$v_code = '';
	$v_values ='';
	$v_values_record_id = '';
	$v_from_date='';
	$v_to_date='';
	//nieuvv--011008--loc theo tieu chi ngay tiep nhan va ngay tra ket qua
	$v_receive_from_date = '';
	$v_receive_to_date = '';
	$v_appointed_from_date = '';
	$v_appointed_to_date = '';
	//the--end--nieuvv--011008--
#end vunh added 23/04/2008
# begin vunh added 08/04/2008
	if(isset($_REQUEST['ma_ho_so'])&&($_REQUEST['ma_ho_so']!='')) {
		$v_code .= $_REQUEST['ma_ho_so'];
	}
	if(isset($_REQUEST['ho_ten'])&&($_REQUEST['ho_ten']!='')) {
		$v_values .= $_REQUEST['ho_ten'];
		if(isset($_REQUEST['is_sort'])&&($_REQUEST['is_sort'])) {
			$v_values = urldecode($v_values);
		}
	}
	//14102008_leav
	if(isset($_REQUEST['ben_a'])&&($_REQUEST['ben_a']!='')) {
		$v_name_ben_a .= $_REQUEST['ben_a'];
		if(isset($_REQUEST['is_sort'])&&($_REQUEST['is_sort'])) {
			$v_name_ben_a = urldecode($v_name_ben_a);
		}
	}
	if(isset($_REQUEST['ben_b'])&&($_REQUEST['ben_b']!='')) {
		$v_name_ben_b .= $_REQUEST['ben_b'];
		if(isset($_REQUEST['is_sort'])&&($_REQUEST['is_sort'])) {
			$v_name_ben_b = urldecode($v_name_ben_b);
		}
	}
	//14102008
	if(isset($_REQUEST['can_bo_thu_ly'])&&($_REQUEST['can_bo_thu_ly']!='')) {
		$v_handler_name = $_REQUEST['can_bo_thu_ly'];
                if(isset($_REQUEST['is_sort'])&&($_REQUEST['is_sort'])) {
			$v_handler_name = urldecode($v_handler_name);
		}
	}	
	//14102008
	//--nieuvv--160908--loc them ma ho so ----
	if(isset($_REQUEST['record_id_search'])&&($_REQUEST['record_id_search']!='')) {
		$v_values_record_id .= $_REQUEST['record_id_search'];
	}
	if(isset($_REQUEST['from_receive_date'])&&($_REQUEST['from_receive_date']!='')) {
		$v_from_receive_date = _ddmmyyyy_to_yyyymmdd($_REQUEST['from_receive_date']);
	}
	if(isset($_REQUEST['to_receive_date'])&&($_REQUEST['to_receive_date']!='')) {
		$v_to_receive_date = _ddmmyyyy_to_yyyymmdd($_REQUEST['to_receive_date']);
	}
	if(isset($_REQUEST['from_appointed_date'])&&($_REQUEST['from_appointed_date']!='')) {
		$v_from_result_date = _ddmmyyyy_to_yyyymmdd($_REQUEST['from_appointed_date']);
	}
	if(isset($_REQUEST['to_appointed_date'])&&($_REQUEST['to_appointed_date']!='')) {
		$v_to_result_date = _ddmmyyyy_to_yyyymmdd($_REQUEST['to_appointed_date']);
	}
	//--the end nieuvv---160908---
	if(isset($_REQUEST['tu_ngay'])&&($_REQUEST['tu_ngay']!='')) {
		$v_from_date .= $_REQUEST['tu_ngay'];
	}
	if(isset($_REQUEST['den_ngay'])&&($_REQUEST['den_ngay']!='')) {
		$v_to_date .= $_REQUEST['den_ngay'];
	}
	

#end vunh added 08/04/2008
//echo 'v_page' . $v_page . '<br>';
//echo 'v_limit_row' . $v_limit_row . '<br>';
if(_is_sqlserver()){
	$v_recordtype_filter_ = $v_recordtype_filter;
	//Leav:17092008
	$sql_single_config="Exec Onegate_ConfigRecordtypeGetSingle ";
	$sql_single_config=$sql_single_config."'".$v_recordtype_filter."'";
	$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
	$arr_single_config = $ado_conn->GetRow($sql_single_config); 
	$v_error = _replace_bad_char(trim($rs['RET_ERROR']));
	$v_c_system_conf=$arr_single_config['C_SYSTEM_CONFIG'];
	//Leav_18092008
	$sql_getSingleRecordtype="Onegate_RecordtypeGetSingle_leav ";
	$sql_getSingleRecordtype=$sql_getSingleRecordtype."'".$v_recordtype_filter."'";
	$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
	$arr_singleRecortypes = $ado_conn->GetRow($sql_getSingleRecordtype); 
	$v_error = _replace_bad_char(trim($rs['RET_ERROR']));
	$v_list_leader_last_approve=$arr_singleRecortypes['FK_APPROVE_LEADER_LIST'];
	$v_arrs_leader_last_approve=explode(",", $v_list_leader_last_approve);
	//echo $v_list_leader_last_approve.'<br/>';
	//var_dump(is_leader_last('2233',$v_arrs_leader_last_approve));
	//Leav_18092008_end
	//Leav_23092008---------------chua_commit_dc
	if(($fuseaction=='DISPLAY_ALL_WATING_NOTIFY_TAX' ||$fuseaction=='DISPLAY_ALL_WATING_NOTIFY_PUNISH' ||$fuseaction=='DISPLAY_ALL_WATING_NO_NOTIFY_PUBNISH') && !is_contain_config($v_c_system_conf,'TRINH_LDLC_TP')){
		if($fuseaction=='DISPLAY_ALL_WATING_NOTIFY_TAX'){
			$v_fuseaction_onclick = "DISPLAY_ALL_APPROVING_RECORD_FOR_UPDATE_STATUS_LEAV";
		}elseif($fuseaction=='DISPLAY_ALL_WATING_NOTIFY_PUNISH'){
			$v_fuseaction_onclick='DISPLAY_ALL_APPROVING_RECORD_FOR_UPDATE_STATUS_PUNISH_LEAV';
		}else{
			$v_fuseaction_onclick='DISPLAY_ALL_APPROVING_RECORD_FOR_UPDATE_STATUS_NO_PUNISH_LEAV';
		}
	}else{
		if($fuseaction=='DISPLAY_ALL_WATING_NOTIFY_TAX' && is_leader_last($_SESSION['staff_id'],$v_arrs_leader_last_approve)){
			$v_fuseaction_onclick = "DISPLAY_ALL_APPROVING_RECORD_FOR_UPDATE_STATUS_LEAV";
		}elseif($fuseaction=='DISPLAY_ALL_WATING_NOTIFY_PUNISH' && is_leader_last($_SESSION['staff_id'],$v_arrs_leader_last_approve)){
			$v_fuseaction_onclick='DISPLAY_ALL_APPROVING_RECORD_FOR_UPDATE_STATUS_PUNISH_LEAV';
		}elseif($fuseaction=='DISPLAY_ALL_WATING_NO_NOTIFY_PUBNISH' && is_leader_last($_SESSION['staff_id'],$v_arrs_leader_last_approve)){
				$v_fuseaction_onclick='DISPLAY_ALL_APPROVING_RECORD_FOR_UPDATE_STATUS_NO_PUNISH_LEAV';
		}else{
			$v_fuseaction_onclick=$v_fuseaction_onclick;
		}
	
	}
	//Leav_23092008_end
	
	///leav
	if($v_recordtype_filter=="All")
		for($i=0;$i<sizeof($arr_all_recordtype);$i++)
			if($i==0) $v_recordtype_filter_ = $arr_all_recordtype[$i][0];
			else $v_recordtype_filter_ .= ','.$arr_all_recordtype[$i][0];


		$v_is_approved_license = 0;			
			
	// HieuDTA 04/08/08		
	/*if($_ISA_CURRENT_MODUL_CODE=="HANDLE")
		{
		if ($v_is_granted_view_record!=true){
		$v_detail_status_compare=$v_detail_status_compare.' and fk_handler='.$v_staff_id;
		}
	}*/
		//$v_detail_status_compare=$v_detail_status_compare.' and fk_handler='.$v_staff_id;
		$sql = "Exec Onegate_RecordGetAllForSearchFunctionByName_nieuvv_111108";
			$sql = $sql . " " . $v_staff_id ;
			$sql = $sql . ",'" . $v_recordtype_filter_. "'" ;
			$sql = $sql . ",'" ._ddmmyyyy_to_yyyymmdd($v_receive_date_filter) . "'" ;
			$sql = $sql . ",'" . $v_status_list."'" ;
			$sql = $sql . ",'" . $v_role."'" ;
			$sql = $sql . "," . $v_status_of_handover;
			$sql = $sql . "," . $v_status_of_assigning;
			$sql = $sql . "," . $v_status_of_handle_approve;
			$sql = $sql . "," . $v_status_of_chaiman_approve;
			$sql = $sql . ",'" . $v_order_clause."'" ;
			$sql = $sql . ",'". $v_detail_status_compare."'" ;
			$sql = $sql . "," . $v_page;
			$sql = $sql . "," . $v_limit_row;
			$sql = $sql . "," . $v_status_compare;
			$sql = $sql . ",'" . GetStringToSearch($v_values)."'" ;
			//Leav_14102008_begin
			$sql = $sql . ",'" . GetStringToSearch($v_name_ben_a) ."'" ;
			$sql = $sql . ",'" . GetStringToSearch($v_name_ben_b) ."'" ;			
			//Leav_14102008_end		
			$sql = $sql . ",'" . $v_values_record_id."'" ;	
//----------------------nieuvv--011008--loc theo tieu chi ngay tiep nhan va ngay hen tra-------
			$sql = $sql . ",'" .$v_from_receive_date . "'" ;
			$sql = $sql . ",'" .$v_to_receive_date . "'" ;
			$sql = $sql . ",'" .$v_from_result_date . "'" ;
			$sql = $sql . ",'" .$v_to_result_date . "'" ;
			$sql = $sql . ",0";
			$sql = $sql . "," .$v_type_spec_work ;
			//echo 	$sql;echo $v_order_clause;
//-------------------the--end--nieuvv--011008-----	
			createTempTableForSearchProcedure_Leav_PheDuyet($v_staff_id,$v_recordtype_filter_,$v_from_date,$v_to_date,$v_values,$v_order_clause,$v_page,$v_limit_row,$v_code);

	
		//echo $sql;
		$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);		 
		$arr_all_record = $ado_conn->GetAll($sql);
		//var_dump($arr_all_record);
		//[Leav
		$sql = "Exec Onegate_RecordWorkSingleByRecordtype_leav  ";
		$sql = $sql ."'" . $v_recordtype_filter . "'";
		$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
		$result = $ado_conn->GetAll($sql);
		$v_size=sizeof($result);
		//echo $sql;
//		dropTempTableForSearchProcedure();
		dropTempTableForSearchProcedure_DatCQ_150908();

	//Tong so ho so
		$v_total_record = $arr_all_record[0]['TOTAL_RECORD'];

	
	//So ho so tra ve 
		$v_count = sizeof($arr_all_record);
	for($i=0;$i<$v_count;$i++){
		$arr_all_record[$i]['C_RECEIVED_RECORD_XML_DATA'] = stripslashes($arr_all_record[$i]['C_RECEIVED_RECORD_XML_DATA']);
		$arr_all_record[$i]['C_LICENSE_XML_DATA'] = stripslashes($arr_all_record[$i]['C_LICENSE_XML_DATA']);
	}
	// nieuvv--031008---lay so ngay thuc hien de thong bao ho so sap den han--
	$sql = "Exec ONEGATE_ListGetAllbyCode 'TB_SAP_QH'";
	$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
	$arr_report_date = $ado_conn->GetAll($sql);
	// the end nieuvv--031008--
}
// begin added by vunh 255042008
function get_current_record_type($p_recordtype_id, $p_arr_all_recordtype) {
	if(sizeof($p_arr_all_recordtype) > 0){
		if ($p_recordtype_id !=""){
			for($i=0; $i< sizeof($p_arr_all_recordtype); $i++){
				if ($p_arr_all_recordtype[$i]['PK_RECORDTYPE'] == $p_recordtype_id ){
					return $p_arr_all_recordtype[$i];
				}
			}			
		}
	}
}

function is_handle_leader_first_time($p_staff_id, $current_record_type) {
	return _list_have_element($current_record_type['FK_HANDLE_LEADER_FIRST_TIME'],$p_staff_id,',');
}

function is_handle_leader($p_staff_id, $current_record_type) {
	return _list_have_element($current_record_type['FK_HANDLE_LEADER'],$p_staff_id,',');
}

function is_approve_leader($p_staff_id, $current_record_type) {
	//echo $p_staff_id."</br>";
	//var_dump($current_record_type);exit;
	return _list_have_element($current_record_type['FK_APPROVE_LEADER_LIST'],$p_staff_id,',');
}
// end added by vunh 25042008
?>


